import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.utils.Timer;

import haxe.FastList;
import haxe.Timer;

import nme.Assets;
import TileModel;
import HealthBarView;
import BaseLevel;
import Kongregate;

import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;

class Level1 extends BaseLevel
{
  
  private var _pinkMap:Array<Array<Int>>;  
  public var lastDoor:Int;
  private static inline var MAX_TILE_SIZE:Int = 64;
  private static inline var MAP_COLUMNS:Int = 20;
  private static inline var MAP_ROWS:Int = 8;
  
  //tile ID s in tile sheet
  private static inline var BOX:Int = 00;
  private static inline var SKY:Int = 10;
  private static inline var PLATFORM:Int = 11;
  private static inline var MACHINE_GUN:Int = 01;
  private static inline var PISTOL:Int = 02;
  private static inline var SHOTGUN:Int = 03;
  private static inline var DOOR:Int = 13;

  private static inline var PINK:Int = 21;
  private static inline var BLUE:Int = 20;

  private static inline var PinkRadius:Float = 20.5;
  private static inline var BlueRadius:Float = 20.5;


  private var _SpriteLayer:Sprite;
  public var DoorHeader1:TextField;
  public var DoorHeader2:TextField;
  public var DoorHeader3:TextField;
  public var DoorHeader4:TextField;
  public var DoorHeader5:TextField;
  public var Title:TextField;
  public var Credits:TextField;
  public var Enter:TextField;

  public function new(stage:Dynamic){
  super(stage);
  MAP_COLUMNS = 20;
    removeChild(_staticBackgroundBitmap);
    removeChild(_backgroundBitmap);
    removeChild(_foregroundBitmap);
  _staticBackgroundBitmapData = Assets.getBitmapData("Cardboard_Background.png");
  _staticBackgroundBitmap = new Bitmap(_staticBackgroundBitmapData);

  _backgroundBitmapData = new BitmapData(MAP_COLUMNS * MAX_TILE_SIZE, 
          MAP_ROWS * MAX_TILE_SIZE, true, 0);
  _backgroundBitmap = new Bitmap(_backgroundBitmapData);

  _foregroundBitmapData = new BitmapData(MAP_COLUMNS * MAX_TILE_SIZE, 
          MAP_ROWS * MAX_TILE_SIZE, true, 0);
  _foregroundBitmap = new Bitmap(_foregroundBitmapData);
  _camera = new Rectangle (0, 0, stage.stageWidth, stage.stageHeight);

  _SpriteLayer = new Sprite();
  _SpriteLayer.graphics.beginFill(0x0000000,0);
  _SpriteLayer.graphics.drawRect(0,0,MAP_COLUMNS * MAX_TILE_SIZE,MAP_ROWS * MAX_TILE_SIZE);
  _SpriteLayer.visible = true;

    addChild(_staticBackgroundBitmap);
    addChild(_backgroundBitmap);
    addChild(_SpriteLayer);
    addChild(_foregroundBitmap);

  _platformMap = 
	[
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM]
	];
  _gameObjectMap = 
	[
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	    [-1,-1,BOX,DOOR,-1,-1,DOOR,-1,-1,DOOR,-1,-1,DOOR,-1,-1,DOOR,-1,-1,-1,-1],
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	  ]  ;


  buildReserveEnemies();

DoorHeader1 = new TextField();
DoorHeader1.htmlText ="Circle Collisions";
DoorHeader1.x = 186;DoorHeader1.y = 360;
DoorHeader1.width = 82;DoorHeader1.height = 24;
_SpriteLayer.addChild(DoorHeader1);

DoorHeader2 = new TextField();
DoorHeader2.htmlText ="Square Collisions";
DoorHeader2.x = 378;DoorHeader2.y = 360;
DoorHeader2.width = 82;DoorHeader2.height = 24;
_SpriteLayer.addChild(DoorHeader2);

DoorHeader3 = new TextField();
DoorHeader3.htmlText ="Circle vs Square";
DoorHeader3.x = 570;DoorHeader3.y = 360;
DoorHeader3.width = 82;DoorHeader3.height = 24;
_SpriteLayer.addChild(DoorHeader3);


DoorHeader4 = new TextField();
DoorHeader4.htmlText ="Circle vs Line";
DoorHeader4.x = 762;DoorHeader4.y = 360;
DoorHeader4.width = 82;DoorHeader4.height = 24;
_SpriteLayer.addChild(DoorHeader4);

DoorHeader5 = new TextField();
DoorHeader5.htmlText ="Collision Arrays";
DoorHeader5.x = 954;DoorHeader5.y = 360;
DoorHeader5.width = 82;DoorHeader5.height = 24;
_SpriteLayer.addChild(DoorHeader5);

Title = new TextField();
var myFormat:TextFormat = new TextFormat();
myFormat.align = TextFormatAlign.CENTER;

Title.defaultTextFormat = myFormat;
Title.htmlText ="<center><font size='24'>Collision Detection and You!</font></center>";
Title.x = 0;Title.y = 0;
Title.width = 1280;Title.height = 48;


_SpriteLayer.addChild(Title);

Credits = new TextField();
var myFormat:TextFormat = new TextFormat();
myFormat.align = TextFormatAlign.CENTER;

Credits.defaultTextFormat = myFormat;
Credits.htmlText ="<center><font size='16'>Music is 'Virtutes Instrumenti' by Kevin MacLeod (incompetech.com)
Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/
Everything else by zerocreativity1</font></center>";
Credits.x = 0;Credits.y = 55;
Credits.width = 1280;Credits.height = 88;


_SpriteLayer.addChild(Credits);

Enter = new TextField();
var myFormat:TextFormat = new TextFormat();
myFormat.align = TextFormatAlign.CENTER;

Enter.defaultTextFormat = myFormat;
Enter.htmlText ="<font size='16'>Press Enter when next to a door.</font>";
Enter.x = 0;Enter.y = 192;
Enter.width = 1280;Enter.height = 48;


_SpriteLayer.addChild(Enter);
  // myTimer = new Timer(12);
  //   myTimer.addEventListener("timer", OnEnter);
  //   myTimer.start();
  //Add the game bitmaps
			  
			  //Run the buildMap method to convert the
			  //map array data into a visual display
			  

      

     

    }
public function GetExit():String
{
  if(!_boxModel.exit)
    {return "";}
  var Door1 = new SquareModel();
  Door1.xPos = 3*64;
  Door1.yPos = 6*64;
  Door1.width = 64;
  Door1.height = 64;
  var boxSquare = new SquareModel();
  boxSquare.xPos = _boxModel.xPos;
  boxSquare.yPos = _boxModel.yPos;
  boxSquare.width = _boxModel.width;
  boxSquare.height = _boxModel.height;
  if(_collisionController.squareModelCollision(Door1,boxSquare))
  {
    lastDoor = 1;
    return "Circle";
  }
  var Door2 = new SquareModel();
  Door2.xPos = 6*64;
  Door2.yPos = 6*64;
  Door2.width = 64;
  Door2.height = 64;
  if(_collisionController.squareModelCollision(Door2,boxSquare))
  {
    lastDoor = 2;
    return "Square";
    //return "";
  }

  var Door3 = new SquareModel();
  Door3.xPos = 9*64;
  Door3.yPos = 6*64;
  Door3.width = 64;
  Door3.height = 64;
  if(_collisionController.squareModelCollision(Door3,boxSquare))
  {
    lastDoor = 3;
    return "CircleSquare";
    //return "";
  }

  var Door4 = new SquareModel();
  Door4.xPos = 12*64;
  Door4.yPos = 6*64;
  Door4.width = 64;
  Door4.height = 64;
  if(_collisionController.squareModelCollision(Door4,boxSquare))
  {
    lastDoor = 4;
    return "CircleLine";
  }

  var Door5 = new SquareModel();
  Door5.xPos = 15*64;
  Door5.yPos = 6*64;
  Door5.width = 64;
  Door5.height = 64;
  if(_collisionController.squareModelCollision(Door5,boxSquare))
  {
    lastDoor = 5;
    return "CollisionArray";
  }
  return "";
}
override function OnEnter(e:flash.events.Event)
{
  super.OnEnter(e);
  var level = GetExit();
  if(level != "")
  {
	try
	{
	  kongVar.SubmitStat("NewLevel",1);
	}
	catch (e:Dynamic)
	{
	
	}
	_boxModel.exit = false;
    	UnLoad();
	this.removeEventListener(Event.ENTER_FRAME, OnEnter);
	var requestLevel = new RequestLevelEvent("RequestLevel",level);
	dispatchEvent(requestLevel);return;
  }
  _boxModel.exit = false;
  _SpriteLayer.scrollRect = _camera;
}
public override function Load(inVolume:Bool,inSound:Bool,inKongVar:CKongregate,inDifficulty:Int)
{
 super.Load(inVolume,inSound,inKongVar,inDifficulty);
 buildMap(_platformMap);
 buildMap(_gameObjectMap);
 _boxModel.setX = (lastDoor * 3 +2)* MAX_TILE_SIZE;
}


  public override function buildReserveEnemies()
  {
  }
}